﻿#region [ConsoleAppMain Using Statements]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Runtime.InteropServices;
using System.Diagnostics;
using AccelerometerServer.Services;
#endregion

namespace AccelerometerServer
{
    /// <summary>
    /// Main of Console application
    /// </summary>
    public class ConsoleAppMain
    {
        #region [ConsoleAppMain Extern Methods]
        [DllImport("user32.dll")]
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        
        [DllImport("user32.dll")]
        static extern bool CloseWindow(IntPtr hWnd);
        #endregion

        #region [ConsoleAppMain Main Method]
        /// <summary>
        /// Main for the Console App. It's the first method executed
        /// </summary>
        /// <param name="args">Console line arguments</param>
        /// <remarks>
        /// The console app is the server that will bridge all the silverlight OOB app (Control Panel),
        /// the XNA app (WP7 XNA Accelerometer Simulator) and the Windows Phone Class Libraray
        /// (AccelerometerWP7ClassLibrary).
        /// </remarks>
        public static void Main(string[] args)
        {   
            Console.Title = "Accelerometer Server";
            IntPtr hWnd = FindWindow(null, "Accelerometer Server");
            
            if (hWnd != IntPtr.Zero)
            {
                /* 
                 * 1 to show, 0 to hide 
                 * It should be hidden but there seems to be a problem
                 * closing a hidden console app. The method CloseAccelerometerKit
                 * at WP7_XNA_Accelerometer_Simulator.Utils.Interoperability.ProcessUtil.cs
                 * won't close the console app if it's hidden -.-'
                 * So for now let it show...
                 */
                ShowWindow(hWnd, 1); 
            }

            ServiceHost accelService = new ServiceHost(typeof(AccelerometerService));
            ServiceHost CAPService = new ServiceHost(typeof(CAPService));

            try
            {
                CAPService.Open();
                accelService.Open();
                Console.WriteLine("Accelerometer service is running at:   {0}", accelService.BaseAddresses[0].AbsoluteUri);
                Console.WriteLine("Press <ENTER> to terminate the service..");
                Console.ReadLine();
                CAPService.Close();
                accelService.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An exception occured while creating the serivce. {0}", ex.Message);
                accelService.Abort();
                CAPService.Abort();
            }
        }
        #endregion
    }
}
